#!/bin/bash

DB_NAME=MyBlog.sqlite.bck
DB_NAME_BAK=MyBlog.sqlite.bak
DB_DIR=~/
DB_DIR_BAK=~/

if ! [ -e "${DB_DIR}${DB_NAME}" ]; then
	logger "[BD_BACKUP] ${DB_DIR}${DB_NAME} is not a db file!" 
	exit 1
fi

if ! [ -d "${DB_DIR_BAK}" ]; then
	logger "[DB_BACKUP] $DB_DIR_BAK is not a dir!"
	exit 1
fi

# db is newer than backedup db
if  [ "${DB_DIR}${DB_NAME}" -nt "${DB_DIR_BAK}${DB_NAME_BAK}" ]; then
	NOW=$(date +"%F")
	FILENAME="${NOW}.tar.gz"
	
	echo $FILENAME

	cp -f "${DB_DIR}${DB_NAME}" "${DB_DIR_BAK}${DB_NAME_BAK}"
	cd $DB_DIR_BAK	
	tar -zcvf ${FILENAME} $DB_NAME_BAK
	cat $FILENAME | uuencode $FILENAME | /usr/sbin/sendmail -f admin@ddup.us zhpeng.is@gmail.com
	logger "[DB_BACKUP] send a new copy!"
else
	logger '[DB_BACKUP] it is the latest copy!'
fi
